<?php
//--theme option
global $image_default;
$image_default = get_template_directory_uri().'/img/work-slider.jpg';
//include javascript and css in admin penal.
function boot_custom_format() { 
global $image_default;
?>
<style>	
	.custom_preview_image{max-width: 300px;max-height: 120px;}
	.form-table .text{ width: 345px}
	span.del{
		background: white;
		color: red;
		padding: 0px 5px;
		display: inline-block;
		width: 5px;
		float: right;
		cursor: pointer;
		margin-right: 10px;
	}
	.bg-preview li{ float: left; margin: 0px 3px;}
	.bg-preview li img{max-height: 50px}
	.contact-info li label{
		display: inline-block;
		min-width: 120px;
	}
	.del-bg{
		display: block;
		text-align: center;
		color: red;
		opacity: 0.3;
		font-style: italic;
		cursor: pointer;
	}
	.del-bg:hover{
		opacity: 1;
	}
</style>	
<script>
	jQuery(function(jQuery) {
		jQuery('.image_button_priview').live('click',function() {
			var values 	= jQuery('#bg-preview-value');
			var preview = jQuery('.bg-preview');
			tb_show('', 'media-upload.php?type=image&TB_iframe=true');
			window.send_to_editor = function(html) {
				imgurl = jQuery('img',html).attr('src');
				classes = jQuery('img', html).attr('class');
				id = classes.replace(/(.*?)wp-image-/, '');
				
				values.append("<input name=\"bg-supersized[]\" type=\"hidden\" class=\"custom_upload_image\" value=\""+ id +"\" />");
				preview.append("<li><img src=\""+ imgurl +"\" class=\"custom_preview_image\" alt=\"\"/><span class=\"del-bg\">Delete</span></li>");
				
				tb_remove();
			}
			return false;
		});
		
		jQuery('.custom_upload_image_button').live('click',function() {
			formfield = jQuery(this).siblings('.custom_upload_image');
			preview = jQuery(this).siblings('.custom_preview_image');
			tb_show('', 'media-upload.php?type=image&TB_iframe=true');
			window.send_to_editor = function(html) {
				imgurl = jQuery('img',html).attr('src');
				classes = jQuery('img', html).attr('class');
				id = classes.replace(/(.*?)wp-image-/, '');
				formfield.val(id);
				preview.attr('src', imgurl);
				tb_remove();
			}
			return false;
		});
		jQuery('.custom_clear_image_button').live('click',function() {
			var defaultImage = jQuery(this).parent().siblings('.custom_default_image').text();
			jQuery(this).parent().siblings('.custom_upload_image').val('');
			jQuery(this).parent().siblings('.custom_preview_image').attr('src', defaultImage);
			return false;
		});			
		var new_work_slideshow="<tr><td width=\"30%\"><input name=\"work_photo[]\" type=\"hidden\" class=\"custom_upload_image\" value=\"0\" /><img src=\"<?php echo $image_default ?>\" class=\"custom_preview_image\" alt=\"\"/><br /><input class=\"custom_upload_image_button button\" type=\"button\" value=\"Choose Image\" /></td><td width=\"60%\"><label class=\"title\">Desciption</label><br><textarea name=\"slideshow_description[]\" cols=\"60\" rows=\"5\"></textarea></td><td width=\"10%\"><input class=\"del_slideshow button\" type=\"button\" value=\"Delete\" /></td><tr>";
		
		var new_field = "<tr><td class=\"info-label\">Year</td><td><input class=\"text info-value\" name=\"info-work-field[]\" value=\"\" /><input type=\"hidden\" name=\"info-work-value[]\" value=\"Year\" /></td></tr>";
		
		var label_regex = /^([a-zA-Z0-9: _\.\-\+]{2,})+$/;
		jQuery('#add_new_slideshow_work').click(function() {
			jQuery('#product_photos').append(new_work_slideshow);
		});
		jQuery('input[name="btn_add_new_filed"]').click(function(){
			var value = jQuery('input[name="new-field-value"]').val();
			if( label_regex.test(value)){
				jQuery('#info-field-label').append("<li><span class=\"del\">x</span><label>"+ value +"</label></li>");
				jQuery('#work-info').append("<tr><td class=\"info-label\">"+ value +"</td><td><input class=\"text info-value\" name=\"info-work-field[]\" value=\"\" /><input type=\"hidden\" name=\"info-work-value[]\" value=\""+ value +"\" /></td></tr>");
				
			}
			else
				alert("Please input correct field label!");
		});
		jQuery('span.del').live('click', function(){
			var index = jQuery(this).closest('li').index();			
			jQuery(this).closest('li').remove();
			jQuery('#work-info tr').eq(index).remove();
		});
		
		jQuery('span.del-bg').live('click', function(){
			var index = jQuery(this).closest('li').index();			
			jQuery(this).closest('li').remove();
			jQuery('#bg-preview-value input[type="hidden"]').eq(index).remove();
		});
		
		jQuery('.del_slideshow').live('click',function(){				
			jQuery(this).closest('tr').remove();
		});
		
	});
</script>
<?php }
add_action('admin_head', 'boot_custom_format');

//work
if( ! function_exists( 'boot_post_type_work' ) ) {
	function boot_post_type_work() {
		register_post_type( 
			'work',
			array( 
				'labels' => array(
					'name' 					=> __( 'Works Manage', 'work' ),
					'singular_name' 		=> __( 'work', 'work' ),
					'add_new'				=> __( 'Add New', 'work' ),
					'add_new_item'			=> __( 'Add New work', 'work' ),
					'edit'					=> __( 'Edit', 'work' ),
					'edit_item'				=> __( 'Edit work', 'work' ),
					'new_item'				=> __( 'New work', 'work' ),
					'view'					=> __( 'View work', 'work' ),
					'view_item'				=> __( 'View work', 'work' ),
					'search_items'			=> __( 'Search work', 'work' ),
					'not_found'				=> __( 'No work found', 'work' ),
					'not_found_in_trash' 	=> __( 'No work found in Trash', 'work' ),
					'parent'				=> __( 'Parent work', 'work' ),
				),
				'description' 	=> __( 'Create a work.', 'work' ), 
				'public'		=> true,
				'show_ui' 		=> true,
				'publicly_queryable'	=> true,
				'exclude_from_search' 	=> true,
				'menu_position' => 6,
				'hierarchical'	=> true,
				'query_var'		=> true,
				'register_meta_box_cb' => 'boot_init_metaboxe',
				'supports' => array (
					'title', 
					'editor', 
					'custom-fields', 
					'comments', 
					'thumbnail', 
					'author' 
				)
			)
		);	
		
		register_taxonomy('worktype', 'work', array(		
			'hierarchical' => true,		
			'labels' => array(
				'name' 			=> _x( 'Work Type', 'spa' ),
				'singular_name' => _x( 'worktype', 'spa' ),
				'search_items' 	=> __( 'Search Work Type' ),
				'all_items' 	=> __( 'All Work Type' ),
				'parent_item'	=> __( 'Parent Work Type' ),
				'parent_item_colon' => __( 'Parent Work Type:' ),
				'edit_item' 	=> __( 'Edit Work Type' ),
				'update_item' 	=> __( 'Update Work Type' ),
				'add_new_item' 	=> __( 'Add New Work Type' ),
				'new_item_name' => __( 'New Work Type Name' ),
				'menu_name' 	=> __( 'Work Type' ),
			),
			'query_var' => 'worktype',		
			'rewrite' => array(
				'slug' => 'worktype', 
				'with_front' => false, 
				'hierarchical' => true 
			),
			'public'		=> true,
			'show_ui'		=> true,
			'_builtin'	=> true,
		));
		
	}
}
add_action( 'init', 'boot_post_type_work' );
if ( ! function_exists( 'boot_init_metaboxe' ) ) {
	function boot_init_metaboxe() {		
		//add_meta_box( $id, $title, $callback, $post_type, $context, $priority, $callback_args );
		add_meta_box( 'work_intro', __( 'Work Intro', 'boot' ), 'boot_work_metabox_intro', 'work', 'normal', 'high' ); 
		add_meta_box( 'work_info', __( 'Work Info', 'boot' ), 'boot_work_metabox_info', 'work', 'normal', 'high' ); 	
		add_meta_box( 'work_slideshow', __( 'Work Photo', 'boot' ), 'boot_work_metabox_slideshow', 'work', 'normal', 'high' ); 
			
	}	
}
if ( ! function_exists( 'boot_work_metabox_intro' ) ) {
	function boot_work_metabox_intro( $obj = '', $box = '' ) { 
		$supersized = get_post_meta($obj->ID, 'bg-supersized', true);
		$subtitle = get_post_meta($obj->ID, 'work-subtitle', true);
?>
	<table>	
		<tr valign="top">
			<th scope="row"><label>Sub title:</label></th>
			<td>
				<input name="work-subtitle" value="<?php echo $subtitle ?>" style=" width: 300px; " />
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label>Work slideshow:</label></th>
			<td>
				<div id="bg-preview-value">
					<?php 
					if($supersized != NULL)
						foreach($supersized as $value):
							$imgurl = wp_get_attachment_image_src($value, 'thumbnail');
							$bg_preview .= "<li><img src=\"". $imgurl[0] ."\" class=\"custom_preview_image\" alt=\"\"/><span class=\"del-bg\">Delete</span></li>";
							echo "<input name=\"bg-supersized[]\" type=\"hidden\" class=\"custom_upload_image\" value=\"". $value ."\" />";
						endforeach; 
					?>
				</div>
				<ul class="bg-preview"><?=$bg_preview; ?></ul>
				<div class="clear"></div>
				<input class="image_button_priview button" type="button" value="Choose Image" />					
				<p class="discription">We can add new many slideshow image</p>
			</td>				
		</tr>
	</table>
	<?php }
}

if ( ! function_exists( 'boot_work_metabox_slideshow' ) ) {
	function boot_work_metabox_slideshow( $obj = '', $box = '' ) { 
		global $image_default;
		$work_photos = get_post_meta($obj->ID, 'work_photo', true); 
		$slideshow_descriptions = get_post_meta($obj->ID, 'slideshow_description', true);
?>
	<table id="product_photos" class="form-table">
		<tr>
			<td colspan="3" style=" border-bottom: 1px solid #ccc; ">				
				<input id="add_new_slideshow_work" class="button" type="button" value="Add New photo" />
				<span class="custom_default_image" style="display:none"><?php echo $image_default; ?></span>
				<p class="description">Photo of album work.</p>
			</td>
		</tr>
<?php 
	if($work_photos != NULL)
		foreach($work_photos as $key => $photo){
			$image_src = $image_default;
			if ($photo) { $image_src = wp_get_attachment_image_src($photo, 'medium');	$image_src = $image_src[0]; }		
?>
		<tr>			
			<td width="30%">
			<?php																							
			echo '<input name="work_photo[]" type="hidden" class="custom_upload_image" value="'.$photo.'" /><img src="'.$image_src.'" class="custom_preview_image" alt=""/><br /><input class="custom_upload_image_button button" type="button" value="Choose Image" />';
			?>
			</td>	
			<td width="60%">				
				<label class="title">Desciption</label><br>
				
				<textarea name="slideshow_description[]" cols="60" rows="5"><?php echo $slideshow_descriptions[ $key ]?></textarea>
			</td>
			<td width="10%"><input class="del_slideshow button" type="button" value="Delete" /></td>			
		<tr>	
<?php } ?>		
	</table> 
<?php	}
}

if ( ! function_exists( 'boot_work_metabox_info' ) ) {
	function boot_work_metabox_info( $obj = '', $box = '' ) { 
		$info_field = get_post_meta($obj->ID, 'info-work-field', true);
		$info_value = get_post_meta($obj->ID, 'info-work-value', true);		
?>
		<table class="form-table">	
		<tr>
			<td>
				<ul id="info-field-label">
					<li>Location</li>
					<li>Typology</li>
					<li>Year</li>
			<?php 
				if(count($info_value) > 3) 
					for($i = 3; $i< count($info_value); $i++)
						echo "<li><span class=\"del\">x</span><label>$info_value[$i]</label></li>";
			?>
				</ul>
				<input class="" name="new-field-value" value="" />
				<input name="btn_add_new_filed" class="button" type="button" value="Add New Filed" />				
				<p class="description">Add new lable work info</p>				
			</td>
			<td>
				<table id="work-info">					
					<tr>
						<td class="info-label">Location</td>
						<td>
							<input class="text info-value" name="info-work-field[]" value="<?=$info_field[0] ?>" />
							<input type="hidden" name="info-work-value[]" value="Location" />
						</td>
					</tr>
					<tr>
						<td class="info-label">Typology</td>
						<td>
							<input class="text info-value" name="info-work-field[]" value="<?=$info_field[1] ?>" />
							<input type="hidden" name="info-work-value[]" value="Typology" />
						</td>
					</tr>
					<tr>
						<td class="info-label">Year</td>
						<td>
							<input class="text info-value" name="info-work-field[]" value="<?=$info_field[2] ?>" />
							<input type="hidden" name="info-work-value[]" value="Year" />
						</td>
					</tr>
				<?php 
					if(count($info_value) > 3) 
						for($i = 3; $i< count($info_value); $i++) { ?>
						<tr>
							<td class="info-label"><?=$info_value[$i] ?></td>
							<td>
								<input class="text info-value" name="info-work-field[]" value="<?=$info_field[$i] ?>" />
								<input type="hidden" name="info-work-value[]" value="<?=$info_value[$i] ?>" />
							</td>
						</tr>
				<?php } ?>
				</table>
			</td>			
		</tr>			
	</table> <?php
	}
}

/* When the work is saved, saves our custom data */
if ( ! function_exists ( 'boot_save_postdata_work' ) ) {
	function boot_save_postdata_work( $post_id, $post ) {				
		$Product_photo 		= get_post_meta($post_id, 'work_photo', true);	
		$Product_photo_new 	= $_POST['work_photo'];		
		if($Product_photo != $Product_photo_new){
			update_post_meta($post_id, 'work_photo', $Product_photo_new);
		}elseif ('' == $Product_photo && $Product_photo_new) {
			delete_post_meta($post_id, 'work_photo', $Product_photo);
		}	
		
		//description
		$product_desciption 		= get_post_meta($post_id, 'slideshow_description', true);	
		$product_desciption_new 	= $_POST['slideshow_description'];	
		if($product_desciption != $product_desciption_new){
			update_post_meta($post_id, 'slideshow_description', $product_desciption_new);
		}elseif ('' == $product_desciption && $product_desciption_new) {
			delete_post_meta($post_id, 'slideshow_description', $product_desciption);
		}
		
		$work_field 		= get_post_meta($post_id, 'info-work-field', true);	
		$work_field_new 	= $_POST['info-work-field'];	
		if($work_field != $work_field_new){
			update_post_meta($post_id, 'info-work-field', $work_field_new);
		}elseif ('' == $work_field && $work_field_new) {
			delete_post_meta($post_id, 'info-work-field', $work_field);
		}
		
		$work_value 		= get_post_meta($post_id, 'info-work-value', true);	
		$work_value_new 	= $_POST['info-work-value'];	
		if($work_value != $work_value_new){
			update_post_meta($post_id, 'info-work-value', $work_value_new);
		}elseif ('' == $work_value && $work_value_new) {
			delete_post_meta($post_id, 'info-work-value', $work_value);
		}
		
		$supersized 		= get_post_meta($post_id, 'bg-supersized', true);	
		$supersized_new 	= $_POST['bg-supersized'];	
		if($supersized != $supersized_new){
			update_post_meta($post_id, 'bg-supersized', $supersized_new);
		}elseif ('' == $supersized && $supersized_new) {
			delete_post_meta($post_id, 'bg-supersized', $supersized);
		}
		
		$subtitle 		= get_post_meta($post_id, 'work-subtitle', true);	
		$subtitle_new 	= $_POST['work-subtitle'];	
		if($subtitle != $subtitle_new){
			update_post_meta($post_id, 'work-subtitle', $subtitle_new);
		}elseif ('' == $subtitle && $subtitle_new) {
			delete_post_meta($post_id, 'work-subtitle', $subtitle);
		}
		
	}
}
add_action( 'save_post', 'boot_save_postdata_work', 1, 2 );

// Boot setting
function boot_setting_admin() {
	if ( $_GET['page'] == basename(__FILE__) ) {
		if ( 'save' == $_REQUEST['action'] ) {			
			
			foreach($_POST as $key => $value){
				if($key =='submit' || $key == 'action')
					continue;
				update_option($key,$value);
			}			
			//delete_option
			header("Location: admin.php?page=theme-option.php&saved=true");
		die;
	} 
	else if( 'reset' == $_REQUEST['action'] ) {			
			foreach($_POST as $key => $value){
				if($key =='submit' || $key == 'action')
					continue;
				delete_option($key,$value);
			}	
			header("Location: admin.php?page=theme-option.php&reset=true");
			die;
		}
	}
	add_menu_page('boot_option', 'Boot Option', 'administrator', basename(__FILE__), 'boot_setting_content');
}
add_action('admin_menu', 'boot_setting_admin');
function boot_setting_content(){	
	if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$spa_setting.' settings saved.</strong></p></div>';
	if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$spa_setting.' settings reset.</strong></p></div>';
	global $image_default;
	$bg_preview = '';
	$boot_option = array(
		'facebook-api' 		=> '',
		'boot-address' 		=> '',
		'boot-longitude'	=> '',
		'boot-latitude'		=> '', 
		'boot-email'		=> '', 
		'boot-contact-number'	=> '', 
		'facebook-link'		=> '', 
		'linkedin-link'		=> '', 
		'boot-copyright'	=> '', 
		'bg-supersized'		=> array(), 
	);	
	foreach($boot_option as $key => $value){
		$boot_option[ $key ] = get_option( $key );		
	}
	?>
<div class="wrap">
	<div id="icon-options-general" class="icon32"><br></div>
	<h2>Theme Option</h2>
	<p class="description"></p>	
	<form method="post">
		<table class="form-table"><tbody>		
			<tr valign="top">
				<th scope="row"><label>Facebook Api</label></th>
				<td>
					<input name="facebook-api" type="text" id="facebook-api" value="<?=$boot_option['facebook-api'] ?>" class="regular-text">
				</td>				
			</tr>
			<tr valign="top">
				<th scope="row"><label>Contact info </label></th>
				<td>
					<ul class="contact-info">
						<li><label>Address: </label><input name="boot-address" type="text" value="<?=$boot_option['boot-address'] ?>" class="regular-text"></li>
						<li><label>Longitude: </label><input name="boot-longitude" type="text" value="<?=$boot_option['boot-longitude'] ?>" class="regular-text"></li>
						<li><label>Latitude: </label><input name="boot-latitude" type="text" value="<?=$boot_option['boot-latitude'] ?>" class="regular-text"></li>
						<li><label>Email: </label><input name="boot-email" type="text" value="<?=$boot_option['boot-email'] ?>" class="regular-text"></li>
						<li><label>Contact number: </label><input name="boot-contact-number" type="text" value="<?=$boot_option['boot-contact-number'] ?>" class="regular-text"></li>
						<li><label>Facebook</label><input name="facebook-link" type="text" value="<?=$boot_option['facebook-link'] ?>" class="regular-text"></li>
						<li><label>linkedin</label><input name="linkedin-link" type="text" value="<?=$boot_option['linkedin-link'] ?>" class="regular-text"></li>
						<li><label>Copyright</label><input name="boot-copyright" type="text" value="<?=$boot_option['boot-copyright'] ?>" class="regular-text"></li>
					</ul>
				</td>				
			</tr>
			<tr valign="top">
				<th scope="row"><label>Background slideshow:</label></th>
				<td>
					<div id="bg-preview-value">
						<?php 
						if($boot_option['bg-supersized'] != NULL)
							foreach($boot_option['bg-supersized'] as $value):
								$imgurl = wp_get_attachment_image_src($value, 'thumbnail');
								$bg_preview .= "<li><img src=\"". $imgurl[0] ."\" class=\"custom_preview_image\" alt=\"\"/><span class=\"del-bg\">Delete</span></li>";
								echo "<input name=\"bg-supersized[]\" type=\"hidden\" class=\"custom_upload_image\" value=\"". $value ."\" />";
							endforeach; 
						?>
					</div>
					<ul class="bg-preview"><?=$bg_preview; ?></ul>
					<div class="clear"></div>
					<input class="image_button_priview button" type="button" value="Choose Image" />					
					<p class="discription">supersized background image</p>
				</td>				
			</tr>
		</tbody></table>
		
		<p class="submit"><input type="submit" value="Save Changes" class="button-primary" id="submit" name="submit"></p>
		<input type="hidden" name="action" value="save" />
	</form>
</div>
<div class="clear"></div>
<?php }
// create boxmeta of news
// Add the Meta Box
function add_box_option_news() {
    add_meta_box(
		'add_box_option_news', // $id
		'News options', // $title 
		'show_box_option_news', // $callback
		'post', // $page
		'normal', // $context
		'high'); // $priority
}
add_action('add_meta_boxes', 'add_box_option_news');

if ( ! function_exists( 'show_box_option_news' ) ) {
	function show_box_option_news( $obj = '', $box = '' ) { 
		$supersized = get_post_meta($obj->ID, 'bg-supersized', true);		
?>
	<table>			
		<tr valign="top">
			<th scope="row"><label>Work slideshow:</label></th>
			<td>
				<div id="bg-preview-value">
					<?php 
					if($supersized != NULL)
						foreach($supersized as $value):
							$imgurl = wp_get_attachment_image_src($value, 'thumbnail');
							$bg_preview .= "<li><img src=\"". $imgurl[0] ."\" class=\"custom_preview_image\" alt=\"\"/><span class=\"del-bg\">Delete</span></li>";
							echo "<input name=\"bg-supersized[]\" type=\"hidden\" class=\"custom_upload_image\" value=\"". $value ."\" />";
						endforeach; 
					?>
				</div>
				<ul class="bg-preview"><?=$bg_preview; ?></ul>
				<div class="clear"></div>
				<input class="image_button_priview button" type="button" value="Choose Image" />					
				<p class="discription">We can add new many slideshow image</p>
			</td>				
		</tr>
	</table>
<?php
	}
}

?>